package managers;

import business.Estructura;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import util.Codes;
import util.Table;

public class MngEstructura extends Manager {

	public MngEstructura() {
		
	}
	
	public String getLastID(String tipo) {
		String sql = "", id = "0001";
		
		sql = "	SELECT IFNULL(CONCAT(REPEAT('0', 4 - CHAR_LENGTH(MAX(id) + 1)), MAX(id) + 1), '0001')" +
			  " FROM estructura WHERE tipo = '" + tipo + "'";
		
		try {
			rst = stmt.executeQuery(sql);
			if (rst.next()) id = rst.getString(1);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return id;
	}
	
	public String saveEstructura(int id, String tipo, String descripcion) {
		String res = "";
		try {
			cstm = stmt.getConnection().prepareCall("{call spc_saveEstructura(?, ?, ?)}");
			cstm.setInt(1, id);
			cstm.setString(2, tipo);
			cstm.setString(3, descripcion);
			
			rst = cstm.executeQuery();
			if (rst.next()) res = rst.getString(1);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}

    public Estructura getEstructura(int id, String tipo) {
        Estructura estructura = null;
        String sql = "SELECT descripcion FROM estructura WHERE id = " + id + " AND tipo = '" + tipo + "'";

        try {
            rst = stmt.executeQuery(sql);
            if (rst.next()) {
                estructura = new Estructura();
                estructura.setId(id);
                estructura.setTipo(tipo);
                estructura.setDescripcion(rst.getString("descripcion"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(MngEstructura.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return estructura;
    }
	
	public String deleteEstructura(int id, String tipo) {
		String sql = "DELETE estructura.* FROM estructura WHERE id = " + id + " AND tipo = '" + tipo + "'";
		if (execUpdate(sql)) return getLastID(tipo);
		else return null;
	}
	
	public String getRptTable(String tipo) {
		return getTable(tipo, true);
	}
	
    public String getTable(String tipo) {
        return getTable(tipo, false);
    }

	public String getTable(String tipo, boolean report) {
		String sql = "SELECT id, descripcion FROM estructura WHERE tipo = '" + tipo + "'", tbl = "";

		try {
			rst = stmt.executeQuery(sql);
			while (rst.next()) {
				
				tbl += 	"<tr>" +
							"<td>" + Codes.getCode(rst.getString("id"), 4) + "</td>" +
							"<td>" + rst.getString("descripcion") + "</td>";
				if (!report) {			
					tbl +=	"<td class=\"td-butt\">" +
								"<a href=\"JavaScript:;\" title=\"Modificar\" onclick=\"edit" + tipo + "(this);\">" +
									"<img src=\"imgs/page_white_edit.png\" alt=\"Modificar\" />" +
								"</a>" +
							"</td>" +	
							"<td class=\"td-butt\">" +
								"<a href=\"JavaScript:;\" title=\"Eliminar\" onclick=\"del" + tipo + "(this);\">" +
									"<img src=\"imgs/button_cancel.png\" alt=\"Eliminar\" />" +
								"</a>" +
							"</td>";
							}			
				tbl +=	"</tr>";
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return tbl;
	}


    public String getPopup(String tipo, String fields, String putFunc, String putFields) {
        String sql = "SELECT fillZeros(id, 4) AS id, tipo, descripcion FROM estructura WHERE tipo = '" + tipo + "'";

        return Table.getPopup(sql, fields, putFunc, putFields);
    }
}